package com.example.demo.Service;

import com.example.demo.Dao.RecordRepository;
import com.example.demo.Entity.Record;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.List;

@Service

public class AlertService {
    @Resource
    RecordRepository recordRepository;

    @Transactional(rollbackFor = {})
    public boolean isUserThreeDayHighDuringDay(String openId,int day)
    {
       List<Record> recordList =  recordRepository.findAllHighAlertRecordByUserOpenId(openId,day);
       int total = recordList.size();
       int max =  (day/3);
       if (total>=max){
           return true;
       }
        ZoneId zoneId = ZoneOffset.UTC;
       int sum  = 0;
       LocalDate tempDay  = recordList.get(0).getRecordDate().toInstant().atZone(zoneId).toLocalDate();
        for (Record r:recordList
             ) {
            LocalDate eqlDate = r.getRecordDate().toInstant().atZone(zoneId).toLocalDate();
            boolean result =  tempDay.equals(eqlDate);
            if (result)
            {
                sum++;
            }else {
                sum=0;
            }
            if (sum==3)
            {
                return true;
            }
            tempDay  = tempDay.minusDays(1);
        }
        return false;
    }

}
